<?php
//require_once 'Zend/Controller/Action.php';

class MSA_Controller_Action extends Zend_Controller_Action
{
	private $_auth;
	
	private $_controller;
	
	private $_action;
	
	public function init()
	{
		$this->_controller = $this->getRequest()->getControllerName();
		$this->_action = $this->getRequest()->getActionName();
		$this->_auth = Zend_Auth::getInstance();
		$this->initMenu();
		$this->initInfo();
	}
	
	/**
	 * 
	 * 初始化菜单
	 */
	public function initMenu()
	{
		$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml');//读取菜单配置
		$configArr = $config->toArray();
		$topMenu = array();
		$leftMenuTemp = array();
		$leftMenu = array();
		
		/* 顶部菜单 */
		foreach ($configArr as $key => $item) {
			$row = array();
			$row['label'] = $item['label'];
			$row['href'] = $this->generateUrl($item['action'], $item['controller']);
			$row['active'] = false;
			
			if ($item['controller'] == $this->_controller) {
				$row['active'] = true;
				
				if (isset($item['pages'])) {
					$leftMenuTemp = $item['pages'];
				}
			}
			
			$topMenu[] = $row;
		}
		
		/* 左边菜单 */
		foreach ($leftMenuTemp as $item) {
			$row = array();
			$row['label'] = $item['label'];
			$row['pages'] = array();
			
			if (isset($item['pages'])) {
				foreach ($item['pages'] as $subItem) {
					$subRow = array();
					$subRow['label'] = $subItem['label'];
					$subRow['href'] = $this->generateUrl($subItem['action'], $subItem['controller']);
					$subRow['active'] = false;
					if ($this->_action == $subItem['action'] && $this->_controller == $subItem['controller']) {
						$subRow['active'] = true;
					}
					
					$row['pages'][] = $subRow;
				}
			}
			
			$leftMenu[] = $row;
		}
		
		$this->view->topMenu = $topMenu;
		$this->view->leftMenu = $leftMenu;
	}
	
	/**
	 * 
	 * 初始化界面信息,例如：登录信息
	 */
	public function initInfo()
	{
		$this->view->authInfo = $this->getLoginInfo();
	}
	
	/**
	 * 
	 * 设置layout
	 * @param string $layout
	 */
	public function setLayout($layout) 
	{
		$this->_helper->layout()->setLayout($layout);
	}
	
	/**
	 * 
	 * 关闭layout
	 */
	public function disableLayout()
	{
		$this->_helper->layout()->disableLayout();
	}
	
	public function getAuth()
	{
		return $this->_auth;
	}
	
	public function isLogin()
	{
		return $this->_auth->hasIdentity();
	}
	
	public function getLoginInfo()
	{
		if ($this->isLogin()) {
			return $this->_auth->getIdentity();
		}
		return null;
	}
	
	public function getUid()
	{
		$info = $this->getLoginInfo();
		if ($info) {
			return $info->id;
		}
		return null;
	}
	
	public function getUsername()
	{
		$info = $this->getLoginInfo();
		if ($info) {
			return $info->username;
		}
		return null;
	}
	
	/**
	 * 
	 * 生成url
	 * @param string $action
	 * @param string $controller
	 * @param string $module
	 */
	public function generateUrl($action = 'index', $controller = 'index', $module = 'default')
	{
		return $this->view->url(array('action' => $action, 
			'controller' => $controller, 
			'module' => $module));
	}
	
	/**
	 * 
	 * 输出json
	 * @param array $str
	 */
	public function json($str)
	{
		$this->_helper->viewRenderer->setNoRender();//不输出模板
		$data = $this->_helper->json($str, false);
		echo $data;
		exit();
	}
	
	/**
	 * 
	 * 输出png图片
	 * @param string $path
	 */
	public function outputPng($path)
	{
		$this->_helper->viewRenderer->setNoRender();//不输出模板
		Header("Content-type:image/png");
		$content = file_get_contents($path);
		echo $content;
		exit();
	}
	
	/**
	 * 
	 * 获取请求参数
	 * @param string $key
	 * @param mixed $def
	 */
	public function getParam($key, $def = null)
	{
		return $this->getRequest()->getParam($key, $def);
	}
	
	/**
	 * 
	 * 检查验证码是否正确
	 * @param string $word
	 */
	public function checkCaptcha($word) 
	{
		$captchaSession = new Zend_Session_Namespace('captcha');
		if ($captchaSession->word == $word) {
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * 判断post请求
	 */
	public function isPost()
	{
		return $this->getRequest()->isPost();
	}
	
	public function sendClientCookie($name, $value, $expire, $path = '/')
	{
		setcookie($name, $value, $expire, $path);
	}
	
	public function getClientCookie($name, $def = null)
	{
		return isset($_COOKIE[$name])?$_COOKIE[$name]:$def;
	}
	
	public function cleanClientCookie($name)
	{
		setcookie($name, '', time() - 86400);
	}
}